/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */

package org.apache.directory.server.codec.json;


import java.lang.reflect.Type;

import org.apache.directory.shared.ldap.exception.LdapInvalidDnException;
import org.apache.directory.shared.ldap.message.LdapResultImpl;
import org.apache.directory.shared.ldap.message.ReferralImpl;
import org.apache.directory.shared.ldap.message.ResultCodeEnum;
import org.apache.directory.shared.ldap.name.DN;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;


/**
 * TODO LdapResultSerializer.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class LdapResultCodec implements JsonSerializer<LdapResultImpl>, JsonDeserializer<LdapResultImpl>
{

    private static final String ERROR_MESSAGE = "errorMessage";
    private static final String MATCHED_DN = "matchedDn";
    private static final String RESULT_CODE = "resultCode";


    public JsonElement serialize( LdapResultImpl result, Type typeOfSrc, JsonSerializationContext context )
    {
        JsonObject obj = new JsonObject();

        if ( result.getMatchedDn() != null )
        {
            obj.addProperty( MATCHED_DN, result.getMatchedDn().getName() );
        }

        if( result.getErrorMessage() != null )
        {
            obj.addProperty( ERROR_MESSAGE, result.getErrorMessage() );
        }

        ResultCodeEnum code = result.getResultCode();
        int codeVal = -1; // for the cases where result code is null

        if ( code != null )
        {
            codeVal = code.getValue();
        }

        obj.addProperty( RESULT_CODE, codeVal );

        ReferralImpl referral = ( ReferralImpl ) result.getReferral();
        if ( referral != null )
        {
            obj.addProperty( "referral", JsonBuilder.get().toJson( referral ) );
        }

        return obj;
    }


    public LdapResultImpl deserialize( JsonElement element, Type typeOfT, JsonDeserializationContext context )
        throws JsonParseException
    {
        try
        {
            JsonObject json = element.getAsJsonObject();
            
            LdapResultImpl result = new LdapResultImpl();
            
            JsonElement matchedDn = json.get( MATCHED_DN );
            if( matchedDn != null )
            {
                result.setMatchedDn( new DN( matchedDn.getAsString() ) );
            }
            
            int resultCodeVal = json.get( RESULT_CODE ).getAsInt();
            result.setResultCode( ResultCodeEnum.getResultCode( resultCodeVal ) );
            
            JsonElement error = json.get( ERROR_MESSAGE );
            if( error != null )
            {
                result.setErrorMessage( error.getAsString() );
            }
            
            //TODO yet to support Referrals
            
            return result;
        }
        catch( LdapInvalidDnException e )
        {
            throw new JsonParseException( e );
        }
    }

}
